﻿@model ProductDetailsModel.AddToCartModel
@{
    var shoppingCartType = Model.UpdateShoppingCartItemType ?? ShoppingCartType.ShoppingCart;
}
@if (!string.IsNullOrEmpty(Model.UpdatedShoppingCartItemId))
{
    <input asp-for="UpdatedShoppingCartItemId" type="hidden"/>
}
@if (Model.EnteredPrice)
{
    <div class="customer-entered-price mb-3 d-flex w-100 flex-column">
        <div class="price-input">
            <label for=@($"CustomerEnteredPrice_{Model.ProductId}") class="enter-price-label">@Loc["Products.EnterProductPrice"]</label>
            <input type="text" id=@($"CustomerEnteredPrice_{Model.ProductId}") name="CustomerEnteredPrice" value="@Convert.ToInt32(Math.Ceiling(Model.CustomerEnteredPrice))" class="form-control enter-price-input"/>
        </div>
        <div class="price-range pt-1">
            @Model.CustomerEnteredPriceRange
        </div>
    </div>
}
<div role="group" class="input-group product-add-to-cart flex-nowrap">
    @if (!Model.DisableBuyButton || Model.EnteredPrice)
    {
        if (!string.IsNullOrEmpty(Model.MinimumQuantityNotification))
        {
            <div class="min-qty-notification text-danger w-100 mb-2">@Model.MinimumQuantityNotification</div>
        }

        if (!Model.DisableBuyButton)
        {
            @if (Model.AllowedQuantities.Any())
            {
                <select name="EnteredQuantity" id=@($"EnteredQuantity_{Model.ProductId}") asp-items="Model.AllowedQuantities" class="custom-select form-control qty-dropdown mr-2"></select>
                if (!string.IsNullOrEmpty(Model.MeasureUnit))
                {
                    <span class="qty-unit d-flex align-items-center mr-2">
                        @Model.MeasureUnit
                    </span>
                }
            }
            else
            {
                var addId = "addtocart_" + Model.ProductId + "_EnteredQuantity";
                <label for="@addId" class="sr-only">@Loc["ShoppingCart.AddToCart"]</label>
                <div class="qty-controls">
                    <div class="plus" onclick="increaseValue()"></div>
                    <div class="minus" onclick="decreaseValue()"></div>
                </div>
                <input type="number" inputmode="numeric" id=@($"EnteredQuantity_{Model.ProductId}") name="EnteredQuantity" value="1" class="qty-input form-control text-center">
                <script asp-location="Footer">
                        function increaseValue() {
                            var value = parseInt(document.getElementById("@addId").value, 10);
                            value = isNaN(value) ? 0 : value;
                            value++;
                            document.getElementById("@addId").value = value;
                        }
                        function decreaseValue() {
                            var value = parseInt(document.getElementById("@addId").value, 10);
                            value = isNaN(value) ? 0 : value;
                            value < 2 ? value = 2 : '';
                            value--;
                            document.getElementById("@addId").value = value;
                        }
                </script>
                if (!string.IsNullOrEmpty(Model.MeasureUnit))
                {
                    <span class="qty-unit d-flex align-items-center mr-2">
                        @Model.MeasureUnit
                    </span>
                }
            }

            {
                var addToCartText = "";
                if (!string.IsNullOrEmpty(Model.UpdatedShoppingCartItemId) && Model.UpdateShoppingCartItemType is ShoppingCartType.ShoppingCart or ShoppingCartType.OnHoldCart)
                {
                    addToCartText = Loc["ShoppingCart.AddToCart.Update"];
                }
                else
                {
                    addToCartText = Loc["ShoppingCart.AddToCart"];
                    if (Model.IsReservation)
                    {
                        addToCartText = Loc["ShoppingCart.Reservation"];
                    }

                    if (Model.AvailableForPreOrder)
                    {
                        addToCartText = Loc["ShoppingCart.PreOrder"];
                    }

                    if (Model.IsAuction)
                    {
                        addToCartText = Loc["ShoppingCart.BuyNow"];
                    }
                }

                <div class="input-group-append">
                    <button type="button" id="add-to-cart-button-@Model.ProductId" class="btn btn-info w-100 add-to-cart-button d-inline-flex" data-productid="@Model.ProductId" onclick="AxiosCart.addproducttocart_details('@Url.RouteUrl("AddProductDetails", new { productId = Model.ProductId, shoppingCartTypeId = (int)shoppingCartType })', @($"'#product-details-form-{Model.ProductId}'"));return false;">
                        <span>@addToCartText</span>
                    </button>
                </div>
            }
        }
    }
</div>